const omit = require('lodash/omit')
const _values = require('lodash/values')
const _some = require('lodash/values')
const { db } = require('../config')
const { command } = require('../config')
const _ = command
const { obd_collection } = require('../config')
exports.main = async (event, context) => {
  const data = event.data;
  const userInfo = event.userInfo;
  const condition = data.condition || {};
  const pageSize = data.pageSize || 50;
  const noArea = data.noArea;
  const page = data.page || 0;

  const grid = condition.grid;
  const staff = condition.staff;
  const assign = condition.assign;

  const region = condition.region;
  const userGrid = condition.grid;
  const otherCondition = omit(condition, ['assign', 'grid', 'region', 'isNewAdd'])
  let _condi = noArea ? {} : { region, grid }
  if (assign) {
    _condi['staff'] = assign === '1' ? _.neq('') : _.eq('')
    _condi.grid = assign === '2' ? _.in([grid, '']) : grid
  }

  if (condition.hasOwnProperty('isNewAdd')) {
    if (data.isNewAdd == "是") {
      _condi.isNewAdd = data.isNewAdd;
    } else {
      _condi.isNewAdd = _.neq("是");
    }
  }

  Object.keys(otherCondition).forEach(key => {
    otherCondition[key] && (_condi[key] = new RegExp(otherCondition[key], 'i'))
  })

  values = _values(_condi)
  if (!values.some(v => v !== undefined)) {
    _condi = {}
  }
  console.log('查询条件', _condi)
  // const collection = '1order_list2'  ;
  const countRs = await db.collection(obd_collection).where(_condi).count();

  // console.log(lists)
  return { total: countRs.total, };

}